﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace DSLFactory.VSCTDesigner
{
    partial class BitmapsSwimlane
    {
        public override void OnDragOver(DiagramDragEventArgs e)
        {
            base.OnDragOver(e);
            if( e.Data.GetDataPresent("TEXT") )
                e.Effect = DragDropEffects.Link;
        }

        public override void OnDragDrop(DiagramDragEventArgs e)
        {
            if( e.Data.GetDataPresent("Text") )
            {
                using( Transaction transaction = Store.TransactionManager.BeginTransaction("Create new bitmap") )
                {
                    Bitmaps bitmaps = this.Subject as Bitmaps;
                    string currentDirectory = Path.GetDirectoryName(ShellHelper.GetModelFileName(this.Store));
                    string fileName = (string)e.Data.GetData("Text");
                    if( fileName.StartsWith(currentDirectory, StringComparison.CurrentCultureIgnoreCase) )
                        fileName = fileName.Substring(currentDirectory.Length + 1);
                    Bitmap bmp = new Bitmap(Store);
                    bmp.Guid = Guid.NewGuid().ToString("B");
                    bmp.hRef = fileName;
                    bitmaps.Bitmap.Add(bmp);
                    Microsoft.VisualStudio.Modeling.Diagrams.DropTargetContext.Set(transaction, this.Diagram.Id, e.MousePosition, null);
                    transaction.Commit();
                    return;
                }
            }

            base.OnDragDrop(e);
        }
    }
}
